package com.ericsson.resourceplanner.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class ReadingHolidayListExcel {
	private File inputFile;

	private int year;

	

	public void setInputFile(File inputFile)

	{

		this.inputFile = inputFile;

	}

	

	public ArrayList<String> read()

	{

		Workbook wb = null;

		

		//String file_extn = inputFile.substring(inputFile.lastIndexOf(".")+1);

		//System.out.println("file_extn = "+file_extn);

		int districtcol = -1, datecol = -1;

		ArrayList<String> holidays = new ArrayList<String>();

		Cell district = null;

		try

		{

			FileInputStream fis = new FileInputStream(inputFile);

		

			/*if(fis!= null && file_extn.equalsIgnoreCase("xls"))

			{

				wb = new HSSFWorkbook(fis);

			}*/

			/*if(file_extn.equalsIgnoreCase("xlsx"))

			{

				wb = new 

			}	

			*/

			wb = new HSSFWorkbook(fis);

			Sheet sht = wb.getSheetAt(0);

			Iterator<Row> rowIterator = sht.iterator();

			while(rowIterator.hasNext())

			{

				Row row = rowIterator.next();

				Iterator<Cell> cellIterator = row.iterator();

				while(cellIterator.hasNext())

				{

					Cell cell = cellIterator.next();

					

					if(districtcol==-1 && cell.getStringCellValue().equalsIgnoreCase("West Bengal"))

					{

						districtcol = cell.getColumnIndex();

						System.out.println("district column number = "+districtcol);

					}

					if(datecol == -1 && cell.getStringCellValue().equalsIgnoreCase("Date") )

					{

						datecol = cell.getColumnIndex();

						System.out.println("date column index = "+datecol);

					}					

				}

				if(datecol>0 && districtcol>0)

				{

					district = row.getCell(districtcol);

					//System.out.println("district ----->"+district);

					if(district == null)

					{

						break;

					}

					if(district != null && !(district.getStringCellValue().equals("")) && !(district.getStringCellValue().equals("West Bengal")))

					{

						holidays.add(row.getCell(datecol).toString());

					}

				}

			}

			System.out.println("holidays = "+holidays);

		}

		catch(FileNotFoundException fe)

		{

			fe.printStackTrace();

		}

		catch(IOException io)

		{

			io.printStackTrace();

		}

		return holidays;

	}
	

}
